package com.example.test111.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.*;
import com.example.test111.domian.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    ElasticsearchClient client;

    @GetMapping("/auto")
    public String toAuto(){
        return "auto";
    }

    @ResponseBody
    @GetMapping("/autocomplete")
    public List<Student> autocomplete(String name) throws IOException {

        System.out.println("进入自动补全查询方法");

        CompletionSuggester completionSuggester = new CompletionSuggester.Builder().field("university").size(3).build();
        FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix(name).completion(completionSuggester).build();
        Suggester suggester = new Suggester.Builder().suggesters("auto_key", fieldSuggester).build();
        SearchRequest request = new SearchRequest.Builder().index("student2").suggest(suggester).build();
        SearchResponse<Student> response = client.search(request, Student.class);


        List<Student> studentList=new ArrayList<>();

        List<CompletionSuggestOption<Student>> autoKey = response.suggest().get("auto_key").get(0).completion().options();

        for (CompletionSuggestOption<Student> option : autoKey) {
            System.out.println(option.source());
            studentList.add(option.source());
        }
        return studentList;
    }

    @GetMapping("/student")
    public String toStudent(){
        return "search";
    }


    @PostMapping("/getStudents")
    public ModelAndView getStudents(String keyword) throws IOException {
        System.out.println("高亮显示方法");
        HighlightField highlightField = new HighlightField.Builder()
                .preTags("<font color='red'>")
                .postTags("</font>")
                .build();

        Highlight highlight = new Highlight.Builder().fields("name", highlightField).build();
        MatchQuery matchQuery = new MatchQuery.Builder().field("name").query(keyword).build();
        Query query = new Query.Builder().match(matchQuery).build();

        SearchRequest request = new SearchRequest.Builder().index("student2")
                .query(query)
                .highlight(highlight)
                .build();

        SearchResponse<Student> response = client.search(request, Student.class);

        List<Student> studentList=new ArrayList<>();

        for (Hit<Student> hit : response.hits().hits()) {
            Student student=hit.source();
            student.setName(hit.highlight().get("name").get(0));
            studentList.add(hit.source());
        }

        ModelAndView view=new ModelAndView("student");
        view.addObject("studentList",studentList);
        return view;

    }


}
